<?php
/**
 *@todo 订单管理
 +------------------------------------------------------------------------------
 * @version   1.0
 * @since 2015-8-17
 +------------------------------------------------------------------------------
 */
namespace Admin\Controller;
class OrderController extends AdminController {
    
    /**
     * @todo    现场收银
     * @author  fang
     */
    public function cashier(){
        
        if(IS_POST){
            $obj = D('Order');
            $result = $obj->pay();
            if($result){
                $this->success($result);
            }else{
                $this->error($obj->getError());
            }
        }else{
            $activity_id = (C('JJHH_ACTIVITY_ID') > 0)? C('JJHH_ACTIVITY_ID') : 0 ;
            $activity_list = $this->getActivityList(0);
            
            $this->meta_title = '现场收银';
            $this->assign('activity_id', $activity_id);
            $this->assign('activity_list',$activity_list);
            $this->display('cashier');
        }
    }
    
    /**
     * @todo    现场退款
     * @author  fang
     */
    public function refund(){
        $obj = D('Order');
        if(IS_POST){
            $result = $obj->refund();
            if($result){
                $this->success('操作成功');
            }else{
                $this->error($obj->getError());
            }
        }else {
    		$gift_info = '';
        	$paper_order = (int)strip_tags(I('order_number', ''));
        	$order_where = array();
        	if($paper_order!=0){
        		$order_where['paper_order'] = $paper_order;
        		$data = $obj->getOrderPayment($order_where);
        		$is_gift = $obj->isOrderGift($paper_order);
    		    $gift_info = ($is_gift)? '<font color="red">已换礼</font>' : '<font color="green">未换礼</font>' ;
        	}else{
        		$data = null;
        	}
        	$this->assign('order', $data['order']);
        	$this->assign('payment_list', $data['payment']);
        	$this->assign('gift_info', $gift_info);
        	$this->meta_title = '现场退款';
        	$this->display('refund');
        }
    }
    
    /**
     * @todo    订单核销
     * @author  fang
     */
    public function account(){
        
        $activity_id = (int)I('activity_id', 0);
        $merchant_id = (int)I('merchant_id', 0);
        $shop_id = (int)I('shop_id', 0);
        $order_type = (int)I('order_type', 0);
        $order_status = (int)I('order_status', 100);
        $paper_order = strip_tags(I('paper_order', ''));
        $phone_number = strip_tags(I('phone_number', ''));
        $start_time = (I('start_time'))? strtotime(I('start_time')) : 0 ;
        $end_time = (I('end_time'))? strtotime(I('end_time')) : NOW_TIME ;
        $order_id = (int)I('order_id');//订单号
        
        //根据手机号读取用户ID
        $customer_info = array();
        if($phone_number != '' && $phone_number != null){
            $customer_info = D('Admin/Customer')->getCustomerByMobilePhone($phone_number,'customer_id');
        }
        
        //初始化查询条件
        $map = array();
        //$map['order_status'] = array('in', '2,4,5');//2:已付款;4:已消费5:申请核销;
        if($activity_id != 0){
            $map['activity_id'] = $activity_id;
        }
        if($merchant_id != 0){
            $map['merchant_id'] = $merchant_id;
        }
        if($shop_id != 0){
            $map['shop_id'] = $shop_id;
        }
        if($paper_order != '' && $paper_order != null){
            $map['paper_order'] = $paper_order;
        }
        if($customer_info){
            $map['customer_id'] = $customer_info['customer_id'];
        }
        if($order_type > 0){
            $map['order_type'] = $order_type;
        }
        if($order_status != 100){
    		$map['order_status'] = $order_status;
    	}
        if($start_time > 0){
            $map['create_time'] = array('between', array($start_time, $end_time));
        }
        if($order_id != 0){
        	$map['order_id'] = $order_id;
        }
        
        if($map){
            $list = $this->lists(D('OrderShopView'),$map,'`order_id` desc');
        }else{
            $list = null;
        }

        //搜索条件显示
        $activity_list = D('Activity','Service')->getActivityList(0);//活动列表
        if($activity_id != 0){
            $merchant_list = D('Admin/Shop')->getActivityMerchant($activity_id);
        }else{
            $merchant_list = null;
        }
        if($activity_id != 0 && $merchant_id != 0){
            $shop_list = D('Admin/Shop')->getMerchantShop($activity_id,$merchant_id);
        }else{
            $shop_list = null;
        }

        $this->meta_title = '订单核销';
        $this->assign('activity_list',$activity_list);
        $this->assign('merchant_list',$merchant_list);
        $this->assign('shop_list',$shop_list);
        $this->assign('_list', $list);
        $this->display('account');
    }
    
    /**
     * @todo    订单返款
     * @author  fang
     */
    public function back(){
        
        $activity_id = (int)I('activity_id', 0);
        $merchant_id = (int)I('merchant_id', 0);
        $shop_id = (int)I('shop_id', 0);
        $order_type = (int)I('order_type', 0);
        $order_status = (int)I('order_status', 100);
        $paper_order = strip_tags(I('paper_order', ''));
        $server_order_id = strip_tags(I('server_order_id'));//商户单号
        $phone_number = strip_tags(I('phone_number', ''));
        $start_time = (I('start_time'))? strtotime(I('start_time')) : 0 ;
        $end_time = (I('end_time'))? strtotime(I('end_time')) : NOW_TIME ;
        $apply_start_time = (I('apply_start_time'))? strtotime(I('apply_start_time')) : 0 ;
        $apply_end_time = (I('apply_end_time'))? strtotime(I('apply_end_time')) : NOW_TIME ;
        $back_start_time = (I('back_start_time'))? strtotime(I('back_start_time')) : 0 ;
        $back_end_time = (I('back_end_time'))? strtotime(I('back_end_time')) : NOW_TIME ;
        $order_id = (int)I('order_id');//订单号
    	$coupons_type = (int)I('coupons_type', -1);//是否使用优惠卷
        
        //根据手机号读取用户ID
        $customer_info = array();
        if($phone_number != '' && $phone_number != null){
            $customer_info = D('Admin/Customer')->getCustomerByMobilePhone($phone_number,'customer_id');
        }
        //初始化查询条件
        $map = array();
        if($activity_id != 0){
            $map['activity_id'] = $activity_id;
        }
        if($merchant_id != 0){
            $map['merchant_id'] = $merchant_id;
        }
        if($shop_id != 0){
            $map['shop_id'] = $shop_id;
        }
        if($paper_order != '' && $paper_order != null){
            $map['paper_order'] = $paper_order;
        }
        if($customer_info){
            $map['customer_id'] = $customer_info['customer_id'];
        }
        if($order_type > 0){
            $map['order_type'] = $order_type;
        }
        if($start_time > 0){
            $map['create_time'] = array('between', array($start_time, $end_time));
        }
        if($apply_start_time > 0){
            $map['apply_time'] = array('between', array($apply_start_time, $apply_end_time));
        }
        if($back_start_time > 0){
            $map['back_time'] = array('between', array($back_start_time, $back_end_time));
        }
        if($server_order_id != ''){
            $map['server_order_id'] = $server_order_id;
        }
        if($order_id != 0){
        	$map['order_id'] = $order_id;
        }
        if($order_status != 100){
            $map['order_status'] = $order_status;
        }else{
            if($map){
                $map['order_status'] = array('in', '5,7');//5:申请核销;7:已返款;
            }
        }
        if($coupons_type > -1){
            $map['coupons_type'] = $coupons_type;
        }
        
        if($map){
            //输入手机号不是系统用户时
            if($phone_number && empty($customer_info)){
                $list = null;
            }else{
                $list = $this->lists(D('OrderShopView'),$map,'apply_time asc,`order_id` desc');
            }
        }else{
            $list = null;
        }
        
        //搜索条件显示
        $activity_list = D('Activity','Service')->getActivityList(0);//活动列表
        if($activity_id != 0){
            $merchant_list = D('Admin/Shop')->getActivityMerchant($activity_id);
        }else{
            $merchant_list = null;
        }
        if($activity_id != 0 && $merchant_id != 0){
            $shop_list = D('Admin/Shop')->getMerchantShop($activity_id,$merchant_id);
        }else{
            $shop_list = null;
        }
        
        $this->meta_title = '订单返款';
        $this->assign('activity_list',$activity_list);
        $this->assign('merchant_list',$merchant_list);
        $this->assign('shop_list',$shop_list);
        $this->assign('_list', $list);
        $this->display('back');
    }
    
    /**
     * @todo    订单流水
     * @author  fang//PaperOrderView
     */
    public function payment(){

        $activity_id = (int)I('activity_id', 0);
        $merchant_id = (int)I('merchant_id', 0);
        $shop_id = (int)I('shop_id', 0);
        $pay_type = (int)I('pay_type', 0);
        $type = (int)I('type', 0);
        $create_user_id = (int)I('create_user_id', 0);
        $paper_order = strip_tags(I('paper_order', ''));
        $phone_number = strip_tags(I('phone_number', ''));
        $start_time = (I('start_time'))? strtotime(I('start_time')) : 0 ;
        $end_time = (I('end_time'))? strtotime(I('end_time')) : NOW_TIME ;
        $order_id = (int)I('order_id');//订单号
        $amount = floor(I('amount'));//金额
        
        //根据手机号读取用户ID
        $customer_info = array();
        if($phone_number != '' && $phone_number != null){
            $customer_info = D('Admin/Customer')->getCustomerByMobilePhone($phone_number,'customer_id');
        }
        
        //初始化查询条件
        $map = array();
        if($activity_id != 0){
            $map['activity_id'] = $activity_id;
        }
        if($merchant_id != 0){
            $map['merchant_id'] = $merchant_id;
        }
        if($shop_id != 0){
            $map['shop_id'] = $shop_id;
        }
        if($customer_info){
            $map['customer_id'] = $customer_info['customer_id'];
        }
        if($amount > 0){
            $map['amount'] = $amount;
        }
        if($pay_type != 0){
            if($pay_type == 3){//3,5
                $map['pay_type'] = array('in','3,5');
            }else if($pay_type == 6){//6,7
                $map['pay_type'] = array('in','6,7');
            }else{
                $map['pay_type'] = $pay_type;
            }
        }
        //流水类型
        switch ($type){
            case 0:
                //收银（总）
                unset($map['type']);
                if($create_user_id > 0){
                    $map['collect_id'] = $create_user_id;
                }
                break;
            case 1:
                //收银（净）
                $map['type'] = 1;
                if($create_user_id > 0){
                    $map['collect_id'] = $create_user_id;
                }
                break;
            case 2:
                //退款
                $map['type'] = 2;
                if($create_user_id > 0){
                    $map['refund_id'] = $create_user_id;
                }
                break;
        }
        //纸质订单号
        if($paper_order != '' && $paper_order != null){
            $map['paper_order'] = $paper_order;
        }
        //下单时间
        if($start_time > 0){
            $map['create_time'] = array('between', array($start_time, $end_time));
        }
        //订单号
        if($order_id != 0){
        	$map['order_id'] = $order_id;
        }
        if($map){
            //输入手机号不是系统用户时
            if($phone_number && empty($customer_info)){
                $list = null;
            }else{
                $list = $this->lists(D('OrderPaymentView'),$map,'create_time desc, apply_time desc, `order_id` desc');
            }
        }else{
            $list = null;
        }
        
        //搜索条件显示
        $activity_list = D('Activity','Service')->getActivityList(0);//活动列表
        if($activity_id != 0){
            $merchant_list = D('Admin/Shop')->getActivityMerchant($activity_id);
        }else{
            $merchant_list = null;
        }
        if($activity_id != 0 && $merchant_id != 0){
            $shop_list = D('Admin/Shop')->getMerchantShop($activity_id,$merchant_id);
        }else{
            $shop_list = null;
        }
        
        //总计
        $amount_sum = D('Order')->getAmountSum($map);
        $user_list = D('Order')->getUserList(); 
        
        $this->meta_title = '订单流水';
        $this->assign('login_id' ,UID);
        $this->assign('type', $type);
        $this->assign('activity_list',$activity_list);
        $this->assign('merchant_list',$merchant_list);
        $this->assign('shop_list',$shop_list);
        $this->assign('user_list',$user_list);
        $this->assign('amount_sum',(float)$amount_sum);
        $this->assign('_list', $list);
        $this->display('payment');
        
    }
    
    /**
     * @todo    导出订单明细
     * @author  fang
     */
    public function orderExport(){
        
        //获取搜索条件
        $activity_id = (int)I('activity_id');//活动搜索ID号
        $merchant_id = (int)I('merchant_id');//商户搜索ID号
        $shop_id = (int)I('shop_id');//店铺搜索ID号
        $order_type = (int)I('order_type');//订单类型
        $order_status = (int)I('order_status',100);//订单状态
        $order_id = (int)I('order_id');//订单号
        $paper_order = (int)I('paper_order');//纸质订单号
        $phone_number = strip_tags(I('phone_number'));//手机号
        
        //根据手机号读取用户ID
        if(preg_match('/^1[3-8][0-9]{9}$/', $phone_number)){
            $customer_info = D('Admin/Customer')->getCustomerByMobilePhone($phone_number,'customer_id');
        }
        
        //搜索条件显示
        $activity_list = D('Activity','Service')->getActivityList(0);//活动列表
        if($activity_id != 0){
            $merchant_list = D('Admin/Shop')->getActivityMerchant($activity_id);
        }else{
            $merchant_list = null;
        }
        if($activity_id != 0 && $merchant_id != 0){
            $shop_list = D('Admin/Shop')->getMerchantShop($activity_id,$merchant_id);
        }else{
            $shop_list = null;
        }
         
        $map = array();
        //查询条件
        if($activity_id != 0){
            $map['activity_id'] = $activity_id;
        }
        if($merchant_id != 0){
            $map['merchant_id'] = $merchant_id;
        }
        if($shop_id != 0){
            $map['shop_id'] = $shop_id;
        }
        if($order_type != 0){
            $map['order_type'] = $order_type;
        }
        if($order_status != 100){
            $map['order_status'] = $order_status;
        }else{
            $map['order_status'] = array('gt', 1);
        }
        if($paper_order != 0 ){
            $map['paper_order'] = $paper_order;
        }
        if(!empty($customer_info)){
            $map['customer_id'] = $customer_info['customer_id'];
        }
        if($order_id != 0){
            $map['order_id'] = $order_id;
        }
        $list = null;
        //输入手机号不是系统用户时
        if($phone_number && empty($customer_info)){
            $list = null;
        }

        $export_type = 2;
        //收款数据与退款数据－1收款:2退款
        if($export_type == 1){
            //收款
            $export_str = '收款';
        }else{
            //退款
            $export_str = '退款';
            $map['order_refund_amount'] = array('gt', 0);
        }
        // 参数
        $limit = '0,20000';
        $map['create_time'] = array('between', array(1459526400, 1459785600));//4.2-1459526400-4.3-1459612800-4.4-1459699200-4.5-1459785600
        $field = 'order_id,create_time,order_status,shop_name,customer_id,order_refund_amount as order_amount,server_order_id,paper_order';
        // 订单数据
        $list = D('OrderShopView')->field($field)->where($map)->order('order_id desc')->limit($limit)->select();

        $result = array('status' => 0, info => '系统繁忙，请稍后再试');
        if(!$list){
            $this->ajaxReturn($result);
        }
        $data = array();
        //获取订单的付款流水
        $where['order_id'] = array('in', array_column($list, 'order_id'));
        $where['type'] = $export_type;//1收款:2退款
        $payment_list = M('order_payment')->field('order_id,amount,pay_type')->where($where)->select();
        $payment_array = array();
        foreach ($payment_list as $key=>$val){
            $array = array(
                'amount' => $val['amount'],
                'pay_type' => $val['pay_type']
            );
            if(isset($payment_array[$val['order_id']])){
                array_push($payment_array[$val['order_id']], $array);
            }else{
                $payment_array[$val['order_id']][] = $array;
            }
        }
        //获取客户信息
        $customer_ids =  array_merge(array_unique(array_column($list, 'customer_id')));
        $where_customer['customer_id'] = array('in', $customer_ids);
        $customer_list = M('customer')->field('customer_id,mobile_phone')->where($where_customer)->select();
        $customer_array = array_column($customer_list, 'mobile_phone', 'customer_id');
        //订单数据处理
        $order_array = array();
        foreach ($list as $key=>$val){
            $order_array[$key] = array(
                'order_id' => $list[$key]['order_id'],
                'create_time' => $list[$key]['create_time'],
                'order_status' => $list[$key]['order_status'],
                'shop_name' => $list[$key]['shop_name'],
                'mobile_phone' => $customer_array[$list[$key]['customer_id']],
                'order_pay_amount' => '',
            );
            $order_array[$key]['pay_type_1'] = '';
            $order_array[$key]['pay_type_2'] = '';
            $order_array[$key]['pay_type_3'] = '';
            $order_array[$key]['pay_type_4'] = '';
            $order_array[$key]['pay_type_5'] = '';
            $order_array[$key]['pay_type_6'] = '';
            $order_array[$key]['pay_type_7'] = '';
            $order_array[$key]['pay_type_9'] = '';
            if(!isset($payment_array[$val['order_id']])){
                continue;
            }
            $sum = 0;
            foreach ($payment_array[$val['order_id']] as $k=>$v){
                switch ($v['pay_type']){
                    case 1:
                        $order_array[$key]['pay_type_1'] += $v['amount'];
                        $sum += $v['amount'];
                        break;
                    case 2:
                        $order_array[$key]['pay_type_2'] += $v['amount'];
                        $sum += $v['amount'];
                        break;
                    case 3:
                        $order_array[$key]['pay_type_3'] += $v['amount'];
                        $sum += $v['amount'];
                        break;
                    case 4:
                        $order_array[$key]['pay_type_4'] += $v['amount'];
                        $sum += $v['amount'];
                        break;
                    case 5:
                        $order_array[$key]['pay_type_5'] += $v['amount'];
                        $sum += $v['amount'];
                        break;
                    case 6:
                        $order_array[$key]['pay_type_6'] += $v['amount'];
                        $sum += $v['amount'];
                        break;
                    case 7:
                        $order_array[$key]['pay_type_7'] += $v['amount'];
                        $sum += $v['amount'];
                        break;
                    case 9:
                        $order_array[$key]['pay_type_9'] += $v['amount'];
                        $sum += $v['amount'];
                        break;
                    default:
                }
            }
            $order_array[$key]['order_pay_amount'] = $sum;
            $order_array[$key]['server_order_id'] = $list[$key]['server_order_id'];
            $order_array[$key]['paper_order'] = $list[$key]['paper_order'];
        }
        $data = $order_array;
        // 日志
        \Think\Log::write("订单数据导出：操作人： ---".getUserName(UID).'---'.json_encode($map).PHP_EOL);
        $sms_content = '导出订单数据'.count($data).'条，操作人：'.getUserName(UID);
        // 短信
        //D('Api/Sms','Service')->sendSms('13263512111', $sms_content);

        // 表头字段与data中value对应[0]表明[1]宽度
        $cell = array(
            'A' => array('订单号'),
            'B' => array('订单日期',15),
            'C' => array('订单状态'),
            'D' => array('商铺名称',30),
            'E' => array('客户手机',15),
            'F' => array('金额',15),
            'G' => array('POS结算'),
            'H' => array('现金结算'),
            'I' => array('支付宝结算'),
            'J' => array('财付通结算'),
            'K' => array('支付宝wap端结算'),
            'L' => array('微信PC扫码结算'),
            'M' => array('微信JSSDK支付结算'),
            'N' => array('优惠卷结算'),
            'O' => array('商户单号',15),
            'P' => array('纸质订单号',15)
        );
        
        // 修改配置
        if(count($data) > 5000){
            ini_set("memory_limit", "516M");
        }
        // 生成excel
        $is_excel = dataToExcel($cell, $data, '订单数据', '订单数据');
        // 返回
        if($is_excel !== false){
            $result['status'] = 1;
            $result['info'] = $is_excel;
        }
        $this->ajaxReturn($result);
    }
    
//     /**
//      * 1.线上专项区的数据。(10月)
// 	需要 订单号，纸质订单号，识别码，支付网关订单号，店铺名称，品类， 备注中的标题，订单金额，总支付金额，总退款金额
//      */
    public function onlineExport10(){

        $field = 'order_id,conf_category_id,shop_name,paper_order,check_code,server_order_id,remark,order_amount,order_pay_amount,order_refund_amount,order_status';
        $map['activity_id'] = 13;
        $map['shop_id'] = 18;
        $map['order_status'] = array('gt', 1);
        $map['order_pay_amount'] = array('egt', 1);
        //$map['order_status'] = 5;
        $list = D('OrderShopView')->field($field)->where($map)->order('order_id desc')->select();
        $result = array('status' => 0, info => '系统繁忙，请稍后再试');
        if(!$list){
            $this->ajaxReturn($result);
        }
        $data = array();
        foreach ($list as $key=>$val){
            $data[$key] = $val;
            $data[$key]['conf_category_id'] = getConfCategoryName($val['conf_category_id']);
            $json = json_decode($val['remark'],true);
            $data[$key]['remark'] = ($json['info']['goods'])? $json['info']['goods'] : '';
            $data[$key]['order_status'] = get_type_title($val['order_status']);
        }

        // 表头字段与data中value对应[0]表明[1]宽度
        $cell = array(
            'A' => array('订单号'),
            'B' => array('纸质订单号',15),
            'C' => array('识别码'),
            'D' => array('支付网关订单号',15),
            'E' => array('商品名称',15),
            'F' => array('订单金额'),
            'G' => array('总支付金额'),
            'H' => array('总退款金额'),
            'I' => array('订单状态'),
            'J' => array('品类',20),
            'K' => array('店铺名称',30)
        );
        // 生成excel
        $is_excel = dataToExcel($cell, $data, '线上专项区数据', '14th线上专项区数据');
        // 返回
        if($is_excel !== false){
            $result['status'] = 1;
            $result['info'] = $is_excel;
        }
        $this->ajaxReturn($result);
    }
    
    /**
     * 1.线上专项区的数据。(12月)
     需要 订单号，纸质订单号，识别码，支付网关订单号，店铺名称，品类， 备注中的标题，订单金额，总支付金额，总退款金额
     */
    public function onlineExport12(){
    
        $field = 'order_id,conf_category_id,shop_name,paper_order,check_code,server_order_id,goods_id,order_amount,order_pay_amount,order_refund_amount,order_status';
        $map['activity_id'] = -2;
        $map['order_status'] = array('gt', 1);
        //$map['order_status'] = 5;
        $list = D('OrderShopView')->field($field)->where($map)->order('goods_id desc')->select();
        $result = array('status' => 0, info => '系统繁忙，请稍后再试');
        if(!$list){
            $this->ajaxReturn($result);
        }
        $data = array();
        foreach ($list as $key=>$val){
            $data[$key] = $val;
            $data[$key]['conf_category_id'] = getConfCategoryName($val['conf_category_id']);
            $data[$key]['goods_id'] = getGoodsName($val['goods_id']);
            $data[$key]['order_status'] = get_type_title($val['order_status']);
        }
        
        // 表头字段与data中value对应[0]表明[1]宽度
        $cell = array(
            'A' => array('订单号'),
            'B' => array('纸质订单号',15),
            'C' => array('识别码'),
            'D' => array('支付网关订单号',15),
            'E' => array('商品名称',15),
            'F' => array('订单金额'),
            'G' => array('总支付金额'),
            'H' => array('总退款金额'),
            'I' => array('订单状态'),
            'J' => array('品类',20),
            'K' => array('店铺名称',30)
        );
        // 生成excel
        $is_excel = dataToExcel($cell, $data, '线上专项区数据', '15th线上专项区数据');
        // 返回
        if($is_excel !== false){
            $result['status'] = 1;
            $result['info'] = $is_excel;
        }
        $this->ajaxReturn($result);
    }
    
    /**
     * @todo    导出流水账单
     * @author  fang
     */
    public function export(){
        
        $activity_id = (int)I('activity_id', 0);
        $merchant_id = (int)I('merchant_id', 0);
        $shop_id = (int)I('shop_id', 0);
        $pay_type = (int)I('pay_type', 0);
        $type = (int)I('type', 0);
        $create_user_id = (int)I('create_user_id', 0);
        $paper_order = strip_tags(I('paper_order', ''));
        $phone_number = strip_tags(I('phone_number', ''));
        $start_time = (I('start_time'))? strtotime(I('start_time')) : 0 ;
        $end_time = (I('end_time'))? strtotime(I('end_time')) : 0 ;
        $order_id = (int)I('order_id');//订单号
        
        //根据手机号读取用户ID
        $customer_info = array();
        if($phone_number != '' && $phone_number != null){
            $customer_info = D('Admin/Customer')->getCustomerByMobilePhone($phone_number,'customer_id');
        }
        
        //初始化查询条件
        $map = array();
        if($activity_id != 0){
            $map['activity_id'] = $activity_id;
        }
        if($merchant_id != 0){
            $map['merchant_id'] = $merchant_id;
        }
        if($shop_id != 0){
            $map['shop_id'] = $shop_id;
        }
        if($customer_info){
            $map['customer_id'] = $customer_info['customer_id'];
        }
        if($pay_type != 0){
            if($pay_type == 3){//3,5
                $map['pay_type'] = array('in','3,5');
            }else if($pay_type == 6){//6,7
                $map['pay_type'] = array('in','6,7');
            }else{
                $map['pay_type'] = $pay_type;
            }
        }
        //流水类型
        switch ($type){
            case 0:
                //收银（总）
                unset($map['type']);
                if($create_user_id > 0){
                    $map['collect_id'] = $create_user_id;
                }
                break;
            case 1:
                //收银（净）
                $map['type'] = 1;
                if($create_user_id > 0){
                    $map['collect_id'] = $create_user_id;
                }
                break;
            case 2:
                //退款
                $map['type'] = 2;
                if($create_user_id > 0){
                    $map['refund_id'] = $create_user_id;
                }
                break;
        }
        if($paper_order != '' && $paper_order != null){
            $map['paper_order'] = $paper_order;
        }
        //时间判断
        if($start_time > 0 && $start_time < $end_time){
            $map['create_time'] = array('between', array($start_time, $end_time));
        }
        if($order_id != 0){
            $map['order_id'] = $order_id;
        }
        
        $result = array('status' => 0, info => '暂无数据');
        if(!$map){
            $this->ajaxReturn($result);
        }
        
        // 流水数据
        $field = 'paper_order,order_id,shop_id,merchant_id,amount,pay_type,type,create_time';
        $list = D('OrderPaymentView')->field($field)->where($map)->select();
        if($list){
            foreach ($list as $key=>$val){
                $data[$key]['order_id'] = $val['order_id'];
                $data[$key]['paper_order'] = $val['paper_order'];
                $data[$key]['merchant_name'] = getMerchantName($val['merchant_id']);
                $data[$key]['shop_name'] = getShopName($val['shop_id']);
                $data[$key]['amount'] = $val['amount'];
                $data[$key]['pay_type'] = get_pay_title($val['pay_type']);
                $data[$key]['type'] = get_payment_type_title($val['type']);
                $data[$key]['create_time'] = time_format($val['create_time']);
            }
        }
        
        $title = ($create_user_id > 0)? getUserName($create_user_id) : '订单流水' ;
        $sms_content = '导出'.$title.'数据'.count($data).'条，操作人：'.getUserName(UID);
        // 日志
        \Think\Log::write("流水导出：操作人： ---".getUserName(UID)."---".$title.'---'.json_encode($map).PHP_EOL);
        // 短信
        D('Api/Sms','Service')->sendSms('13263512111', $sms_content);
        
        // 表头字段与data中value对应[0]表明[1]宽度
        $cell = array(
            'A' => array('订单号'),
            'B' => array('纸质订单号'),
            'C' => array('商户',20),
            'D' => array('店铺',30),
            'E' => array('金额'),
            'F' => array('支付方式'),
            'G' => array('操作类型'),
            'H' => array('操作时间',20)
        );
        // 生成excel
        $is_excel = dataToExcel($cell, $data, $title, $title);
        // 返回
        if($is_excel !== false){
            $result['status'] = 1;
            $result['info'] = $is_excel;
        }
        $this->ajaxReturn($result);
    }
    
    /**
     * @todo    申请退款管理
     * @author  fang
     */
    public function applyRefund(){

        $activity_id = (int)I('activity_id', 0);
        $merchant_id = (int)I('merchant_id', 0);
        $shop_id = (int)I('shop_id', 0);
        $paper_order = strip_tags(I('paper_order', ''));
        $phone_number = strip_tags(I('phone_number', ''));
        $order_id = (int)I('order_id');//订单号
        //根据手机号读取用户ID
        $customer_info = array();
        if($phone_number != '' && $phone_number != null){
            $customer_info = D('Admin/Customer')->getCustomerByMobilePhone($phone_number,'customer_id');
        }
        //初始化查询条件
        $map = array();
        if($activity_id != 0){
            $map['activity_id'] = $activity_id;
        }
        if($merchant_id != 0){
            $map['merchant_id'] = $merchant_id;
        }
        if($shop_id != 0){
            $map['shop_id'] = $shop_id;
        }
        if($paper_order != '' && $paper_order != null){
            $map['paper_order'] = $paper_order;
        }
        if($customer_info){
            $map['customer_id'] = $customer_info['customer_id'];
        }
        if($order_id != 0){
            $map['order_id'] = $order_id;
        }
        
        if($map){
            $map['order_status'] = 9;//9:申请退款
            //输入手机号不是系统用户时
            if($phone_number && empty($customer_info)){
                $list = null;
            }else{
                $list = $this->lists(D('OrderPaymentView'),$map,'`order_id` desc');
            }
        }else{
            $list = null;
        }
        
        //搜索条件显示
        $activity_list = D('Activity','Service')->getActivityList(0);//活动列表
        if($activity_id != 0){
            $merchant_list = D('Admin/Shop')->getActivityMerchant($activity_id);
        }else{
            $merchant_list = null;
        }
        if($activity_id != 0 && $merchant_id != 0){
            $shop_list = D('Admin/Shop')->getMerchantShop($activity_id,$merchant_id);
        }else{
            $shop_list = null;
        }
        
        $this->meta_title = '申请退款';
        $this->assign('activity_list',$activity_list);
        $this->assign('merchant_list',$merchant_list);
        $this->assign('shop_list',$shop_list);
        $this->assign('_list', $list);
        $this->display('apply_refund');
        
    }
    
    /**
     * @todo    订单状态修改
     * @param   int     id      订单ID
     * @param   string  method  修改状态
     * @author  fang
     */
    public function orderStatus($method = null){
        if ( empty($_REQUEST['id']) ) {
            $this->error('请选择要操作的数据!');
        }
        $id = array_unique((array)I('id',0));
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
    
        $obj = D('order');
        switch ( strtolower($method) ){
            case 'account':
                //核算
                $result = $obj->orderStatus($id, 5);//5:申请核销
                if($result){
                    $this->success('核销成功');
                }else{
                    $this->error($obj->getError());
                }
                break;
            case 'back':
                //返款
                $result = $obj->orderStatus($id, 7);//7:返款
                if($result){
                    $this->success('返款成功');
                }else{
                    $this->error($obj->getError());
                }
                break;
            case 'apply_refund':
                //申请退款
                $result = $obj->orderStatus($id, 9);//9:申请退款
                if($result){
                    $this->success('申请退款成功');
                }else{
                    $this->error($obj->getError());
                }
                break;
            case 'reject':
                //驳回
                $result = $obj->orderStatusReject($id);//原路驳回
                if($result){
                    $this->success('驳回申请成功');
                }else{
                    $this->error($obj->getError());
                }
                break;
                break;
            default:
                $this->error($method.'参数非法');
        }
    }
    
    /**
     * @todo    客户信息查询
     * @author  fang
     */
    public function customerSearch(){
        $phone_number = strip_tags(I('search'), '');
        $activity_id = (int)(I('activity_id', 0));
        if(empty($phone_number)){
            $this->error('【请扫描客户二维码】或【输入客户手机号并回车】');
        }
        if($activity_id == 0){
            $this->error('【请选择活动】');
        }
        if(preg_match('/^1[3-8][0-9]{9}$/', $phone_number) ){
            //手机号
            $customer_id = D('Customer','Service')->customerIsJoinActivity($activity_id,$phone_number,3,0);
            $customer_info = array(
                'phone_number' => $phone_number,
                'customer_id' => $customer_id,
            );
        }else{
            $phone_number = (int)$phone_number;//code_id
//             if($phone_number>=1 && $phone_number<=700){
//             	$this->error('【该身份识别码必须输入手机号】');
//             }
            $code_info = M('ConfCode')->where(array('code_id'=>$phone_number,'activity_id'=>$activity_id))->find();
            if(empty($code_info)){
                $this->error('【用户二维码信息错误】');
                return false;
            }
            $phone = getCustomerName($code_info['customer_id']);
            if($phone){
                $customer_id = (int)$code_info['customer_id'];
                $customer_info = array(
                    'phone_number' => $phone,
                    'customer_id' => $code_info['customer_id'],
                    'customer_name' => getCustomerNameNew($customer_id),
                );
            }else{
                $customer_id = 0;
            }
        }
        if($customer_id > 0){
            $ticket_type = D('Admin/ServiceActivityJoin')->getActivityJoinCustomerShow($activity_id,$customer_id);
            $return['id'] = 'customer_number_help';
            $return['info'] = $customer_info;
            $return['ticket'] = ($ticket_type == 1)? '<font color="red">【VIP票】</font>' : '【普通票】' ;
            $return['type'] = 1;
            $this->success($return);
        }else{
            $this->error('【暂无用户信息】');
        }
    }
    
    /**
     * @todo    纸质订单查询
     * @author  fang
     */
    public function orderSearch(){
        $where['paper_order_num'] = strip_tags(I('search'), '');
        $where['activity_id'] = (int)(I('activity_id', 0));
        if(empty($where['paper_order_num'])){
            $this->error('【请扫描纸质订单】或【输入纸质订单号并回车】');
        }
        $obj = D('Order');
        $data = $obj->getOrder($where);
        if($data){
            $data['conf_category'] = getConfCategoryName($data['conf_category_id']);
            $data['merchant_name'] = getMerchantName($data['merchant_id']);
            $return['id'] = 'order_number_help';
            $return['info'] = $data; 
            $return['type'] = 2;
            $this->success($return);
        }else{
            $this->error('【暂无订单信息】');
        }
    }
    
    /**
     * @todo    支付打印页面（线下）
     * @author  fang
     */
    public function printPay(){
        
        $info = D('Order')->getPrintInfoNew();
        $info['amount_all'] = $info['amount'] = (float)I('amount', 0);
        $info['pay_type'] = (int)I('pay_type', 0);
        $info['coupons_type'] = (int)I('coupons_type', 0);
        $info['coupons_amount'] = (float)I('coupons_amount', 0);
        //本次订单总金额
        if($info['coupons_type'] > 0){
            $info['amount_all'] += $info['coupons_amount'];
        }

        $this->assign('info',$info);
        $this->display('print_pay');
    }
    
    /**
     * @todo    补打页面
     */
    public function printSupply(){
        
        $payment_id = (int)I('payment_id');
        $status = (int)I('status');
        if($payment_id == 0){
            exit('payment_id is null');
        }
        //获取打印信息
        $info = array();
        $data = D('Order')->getPrintSupply($payment_id);
        if($data == false){
            exit('order_data is null');
        }
        //获取订单详情
        $obj = D('Order');
        $info = $obj->orderShopShow($data['order_id']);
		$order_snap = $obj->getOrderSnap($data['order_id']);
        //必须绑定纸质订单
        if($info['paper_order'] == 0){
            exit('paper_order is null');
        }
        $info_data = array_merge($info, $data);
        $this->assign('info', $info_data);
        $this->assign('order_snap', $order_snap);
        switch ((int)$data['order_type']){
            //线下（补打）
            case 1:
                switch ((int)$data['type']){
                    //线下支付
                    case 1:
                        $this->display('print_pay_supply');
                        break;
                    //线下退款
                    case 2:
                        $this->display('print_refund_supply');
                        break;
                }
                break;
            //线上（补打）
            case 2:
                switch ((int)$data['type']){
                    //线上支付
                    case 1:
                        if($status == 1){
                            //申请退款（未退款）
                            $this->display('print_apply_refund_supply');
                        }else{
                            //线上支付补
                            $this->display('print_online_pay_supply');
                        }
                        break;
                    //线上退款申请
                    case 2:
                        //退款（已退款）
                        $this->display('print_apply_refund_supply');
                        break;
                }
                break;
            default:
        }
    }
    
    
    /**
     * @todo    退款打印页面（线下）
     * @author  fang
     */
    public function printRefund(){
    
        $info = D('Order')->getPrintInfoNew();
        
        $this->assign('info',$info);
        $this->display('print_refund');
    }
    
    /**
     * @todo    申请退款打印页面（线上）
     * @author  fang
     */
    public function printApplyRefund(){
    
        $info = D('Order')->getPrintInfoNew();
    
        $this->assign('info',$info);
        $this->display('print_apply_refund');
    }
    
    /**
     * @todo    订单绑定
     * @author  PHP@妖孽 <dino_ma@163.com>
     */
    public function binding(){
        if(IS_POST){
            $order_id = (int)I('order_id');
            $paper_order = (int)I('paper_order');
            $activity_id = (int)I('activity_id');
            $obj = D('Order');
            if($obj->paperOrderBindingOrder($order_id,$paper_order,$activity_id)){
                $this->success('操作成功');
            }else{
                $this->error($obj->getError());
            }
        }else{
            $mobile_phone = trim(I('mobile_phone'));
            $activity_id = (int)I('activity_id');
            //做数据操作
            $map = array();//初始化查询条件
            if($activity_id !=0){
                $map['activity_id']=$activity_id;
            }
            
            if(!preg_match('/^1[3-8][0-9]{9}$/', $mobile_phone) ){
                //通过扫码获取用户信息
                $phone_number = (int)$mobile_phone;//code_id
                if($phone_number!=0){
                    $code_info = M('ConfCode')->where(array('code_id'=>$phone_number,'activity_id'=>$activity_id))->find();
                    if(empty($code_info)){
                        $this->error('用户二维码信息错误');
                        return false;
                    }
                    $mobile_phone = getCustomerName($code_info['customer_id']);
                }
            }
             //         	$customer_service_obj = D('Customer','Service');
             //         	$customer_info = $customer_service_obj->getCustomerByMobilePhone($mobile_phone,'customer_id');
            $list = null;
            if(preg_match('/^1[3-8][0-9]{9}$/', $mobile_phone) && (int)$mobile_phone !=0){
                $customer_id = D('Customer','Service')->customerIsJoinActivity($activity_id,$mobile_phone,3,0);
//                 echo $customer_id;die;
                if($customer_id){
                    $map['customer_id'] = (int)$customer_id;
                    $map['order_type'] =2;//只有线上订单可以绑定.6921168509256
                    $map['order_status']=array('in','2,4');//只拉取线上支付成功的数据
                    $list = $this->lists(M('Order'),$map,'`order_id` desc');
                    //0:订单创建成功待支付;1:支付失败;2:支付成功(已付款);3:已退款;4:申请核销;5:核销完成;6:已结算;
                    //         		$a = M('Order')->select();
                    //         		for($i=0;$i<count($a);$i++){
                    //         			unset($a[$i]['order_id']);
                    //         		}
                    //         		M('Order')->addAll($a);
                    if(!empty($list)){
                        $order_ids = array_column($list, 'order_id');
                        $snpa_data = D('Order')->getOrderSnap($order_ids, 'order_id,goods_name,goods_title');
                        //商品信息
                        $goods_info = array();
                        foreach ($snpa_data as $key=>$val){
                            $goods_info[$val['order_id']] = $val;
                        }
                        foreach ($list as $key=>$val){
                            $list[$key]['goods'] = (isset($goods_info[$val['order_id']]))? (string)$goods_info[$val['order_id']]['goods_name'] : '' ;
                            $list[$key]['title'] = (isset($goods_info[$val['order_id']]))? (string)$goods_info[$val['order_id']]['goods_title'] : '' ;
                        }
                    }
                }
            }
            $this->assign('_list',$list);
          
            $activity_list = $this->getActivityList(0);
            $this->assign('activity_list',$activity_list);
            $this->meta_title = '订单绑定';
            $this->display('binding');
        }
       
    }
    
    /**
     * @todo    纸质订单管理
     * @author  PHP@妖孽<dino_ma@163.com>
     */
    public function sale(){
    	$shop_id = (int)I('shop_id');
    	$activity_id = (int)I('activity_id');
    	$map = array('status'=>array('gt',-1));
    	if($shop_id!=0){
    		$map['shop_id']=$shop_id;
    	}
    	if($activity_id!=0){
    		$map['activity_id']=$activity_id;
    	}
    	if($activity_id !=0 || $shop_id !=0){
    	
    		$list = $this->lists(D('PaperOrderView'),$map,'`paper_order_num` desc ');
    	}else{
    		$list = null;
    	}
    	$activity_list = $this->getActivityList(0);
    	$this->assign('activity_list',$activity_list);
    	$this->assign('_list', $list);
    	$this->assign('activity_list', $activity_list);
        $this->meta_title = '销售纸质订单';
        $this->display('sale');
    }
    
    /**
     * @todo    销售纸质订单
     * @author   PHP@妖孽<dino_ma@163.com>
     */
    public function build(){
    	if(IS_POST){
    		$obj = D('Order');
    		$result = $obj->build();
    		if($result){
    			$this->success('操作成功');
    		}else{
    			$this->error($obj->getError());
    		}
    	}else{
    		$activity_list = $this->getActivityList(0);
    		$this->meta_title = '销售纸质订单';
    		$this->assign('activity_list',$activity_list);
    		$this->display('build');
    	}
    }
    
    /**
     * @todo    订单管理（横向部门）
     */
    public function indexN(){
        cookie('index_type', 'normal');
        //获取搜索条件
        $activity_id = (int)I('activity_id');//活动搜索ID号
        $merchant_id = (int)I('merchant_id');//商户搜索ID号
        $shop_id = (int)I('shop_id');//店铺搜索ID号
        $order_type = (int)I('order_type');//订单类型
        $order_status = (int)I('order_status',100);//订单状态
        $order_id = (int)I('order_id');//订单号
        $paper_order = (int)I('paper_order');//纸质订单号
        $phone_number = strip_tags(I('phone_number'));//手机号
        $coupons_type = (int)I('coupons_type', -1);//是否使用优惠卷
        //时间区间
        $apply_start_time = (I('apply_start_time'))? strtotime(I('apply_start_time')) : 0 ;
        $apply_end_time = (I('apply_end_time'))? strtotime(I('apply_end_time')) : NOW_TIME ;
        $back_start_time = (I('back_start_time'))? strtotime(I('back_start_time')) : 0 ;
        $back_end_time = (I('back_end_time'))? strtotime(I('back_end_time')) : NOW_TIME ;
        
        //根据手机号读取用户ID
        if(preg_match('/^1[3-8][0-9]{9}$/', $phone_number)){
            $customer_info = D('Admin/Customer')->getCustomerByMobilePhone($phone_number,'customer_id');
        }
        
        //搜索条件显示
        $activity_list = D('Activity','Service')->getActivityList(0);//活动列表
        if($activity_id != 0){
            $merchant_list = D('Admin/Shop')->getActivityMerchant($activity_id);
        }else{
            $merchant_list = null;
        }
        if($activity_id != 0 && $merchant_id != 0){
            $shop_list = D('Admin/Shop')->getMerchantShop($activity_id,$merchant_id);
        }else{
            $shop_list = null;
        }
         
        $map = array();
        //查询条件
        if($activity_id != 0){
            $map['activity_id'] = $activity_id;
        }
        if($merchant_id != 0){
            $map['merchant_id'] = $merchant_id;
        }
        if($shop_id != 0){
            $map['shop_id'] = $shop_id;
        }
        if($order_type != 0){
            $map['order_type'] = $order_type;
        }
        if($order_status != 100){
            $map['order_status'] = $order_status;
        }
        if($paper_order != 0 ){
            $map['paper_order'] = $paper_order;
        }
        if(!empty($customer_info)){
            $map['customer_id'] = $customer_info['customer_id'];
        }
        if($order_id != 0){
            $map['order_id'] = $order_id;
        }
        if($coupons_type > -1){
            $map['coupons_type'] = $coupons_type;
        }
        if($apply_start_time > 0){
            $map['apply_time'] = array('between', array($apply_start_time, $apply_end_time));
        }
        if($back_start_time > 0){
            $map['back_time'] = array('between', array($back_start_time, $back_end_time));
        }
        //列表
        if(!empty($map)){
            //输入手机号不是系统用户时
            if($phone_number && empty($customer_info)){
                $list = null;
            }else{
                $list = $this->lists(D('OrderShopView'),$map,'order_id desc');
    	        // 查询日志
				echo action_log('order_select', 'order', UID, UID, $_SERVER['REQUEST_URI']);
            }
        }else{
            $list = null;
        }
        
        //返回值
        $this->assign('s_type', 'normal');
        $this->assign('_list',$list);
        $this->assign('activity_list',$activity_list);
        $this->assign('merchant_list',$merchant_list);
        $this->assign('shop_list',$shop_list);
    	$this->assign('url', U('Order/indexN'));
        $this->meta_title = '订单管理';
        $this->display('index');
    }
    
    /**
     * @todo    订单管理（财务）
     * @author  sap
     */
    public function index(){
        cookie('index_type', null);
    	//获取搜索条件
    	$activity_id = (int)I('activity_id');//活动搜索ID号
    	$merchant_id = (int)I('merchant_id');//商户搜索ID号
    	$shop_id = (int)I('shop_id');//店铺搜索ID号
    	$order_type = (int)I('order_type');//订单类型
    	$order_status = (int)I('order_status',100);//订单状态
    	$order_id = (int)I('order_id');//订单号
    	$paper_order = (int)I('paper_order');//纸质订单号
    	$server_order_id = strip_tags(I('server_order_id'));//商户单号
    	$phone_number = strip_tags(I('phone_number'));//手机号
    	$coupons_type = (int)I('coupons_type', -1);//是否使用优惠卷
        //时间区间
    	$apply_start_time = (I('apply_start_time'))? strtotime(I('apply_start_time')) : 0 ;
    	$apply_end_time = (I('apply_end_time'))? strtotime(I('apply_end_time')) : NOW_TIME ;
    	$back_start_time = (I('back_start_time'))? strtotime(I('back_start_time')) : 0 ;
    	$back_end_time = (I('back_end_time'))? strtotime(I('back_end_time')) : NOW_TIME ;
    
    	//根据手机号读取用户ID
    	if(preg_match('/^1[3-8][0-9]{9}$/', $phone_number)){
    		$customer_info = D('Admin/Customer')->getCustomerByMobilePhone($phone_number,'customer_id');
    	}
    
    	//搜索条件显示
    	$activity_list = D('Activity','Service')->getActivityList(0);//活动列表
    	if($activity_id != 0){
    		$merchant_list = D('Admin/Shop')->getActivityMerchant($activity_id);
    	}else{
    		$merchant_list = null;
    	}
    	if($activity_id != 0 && $merchant_id != 0){
    		$shop_list = D('Admin/Shop')->getMerchantShop($activity_id,$merchant_id);
    	}else{
    		$shop_list = null;
    	}
    	
    	$map = array();
    	//查询条件
    	if($activity_id != 0){
    		$map['activity_id'] = $activity_id;
    	}
    	if($merchant_id != 0){
    		$map['merchant_id'] = $merchant_id;
    	}
    	if($shop_id != 0){
    		$map['shop_id'] = $shop_id;
    	}
    	if($order_type != 0){
    		$map['order_type'] = $order_type;
    	}
    	if($order_status != 100){
    		$map['order_status'] = $order_status;
    	}
    	if($paper_order != 0 ){
    		$map['paper_order'] = $paper_order;
    	}
    	if(!empty($customer_info)){
    		$map['customer_id'] = $customer_info['customer_id'];
    	}
    	if($order_id != 0){
    		$map['order_id'] = $order_id;
    	}
    	if($coupons_type > -1){
    	    $map['coupons_type'] = $coupons_type;
    	}
    	if($apply_start_time > 0){
    	    $map['apply_time'] = array('between', array($apply_start_time, $apply_end_time));
    	}
    	if($back_start_time > 0){
    	    $map['back_time'] = array('between', array($back_start_time, $back_end_time));
    	}
    	if($server_order_id != ''){
    	    $map['server_order_id'] = $server_order_id;
    	}
    	//列表
    	if(!empty($map)){
    	    //输入手机号不是系统用户时
    	    if($phone_number && empty($customer_info)){
    	        $list = null;
    	    }else{
    	        $list = $this->lists(D('OrderShopView'),$map,'order_id desc');
    	        // 查询日志
				echo action_log('order_select', 'order', UID, UID, $_SERVER['REQUEST_URI']);
    	    }
    	}else{
    		$list = null;
    	}
    
    	//返回值
    	$this->assign('_list',$list);
    	$this->assign('activity_list',$activity_list);
    	$this->assign('merchant_list',$merchant_list);
    	$this->assign('shop_list',$shop_list);
    	$this->assign('url', U('Order/index'));
    	$this->meta_title = '订单管理';
    	$this->display('index');
    }
    
    /**
     * @todo    订单管理（商户权限对应）
     * @author  fang 0310
     */
    public function merchantOrder(){
        //获取搜索条件
        $activity_id = (int)I('activity_id');//活动搜索ID号
        $merchant_id = (int)I('merchant_id');//商户搜索ID号
        $shop_id = (int)I('shop_id');//店铺搜索ID号
        $order_type = (int)I('order_type');//订单类型
        $order_status = (int)I('order_status',100);//订单状态
        $order_id = (int)I('order_id');//订单号
        $paper_order = (int)I('paper_order');//纸质订单号
        $phone_number = strip_tags(I('phone_number'));//手机号
        $coupons_type = (int)I('coupons_type', -1);//是否使用优惠卷
        //时间区间
        $apply_start_time = (I('apply_start_time'))? strtotime(I('apply_start_time')) : 0 ;
        $apply_end_time = (I('apply_end_time'))? strtotime(I('apply_end_time')) : NOW_TIME ;
        $back_start_time = (I('back_start_time'))? strtotime(I('back_start_time')) : 0 ;
        $back_end_time = (I('back_end_time'))? strtotime(I('back_end_time')) : NOW_TIME ;
    
        //根据手机号读取用户ID
        if(preg_match('/^1[3-8][0-9]{9}$/', $phone_number)){
            $customer_info = D('Admin/Customer')->getCustomerByMobilePhone($phone_number,'customer_id');
        }
    
        //搜索条件显示
        $activity_list = D('Activity','Service')->getActivityList(0);//活动列表
        if($activity_id != 0){
            $merchant_list = D('Admin/Shop')->getActivityMerchant($activity_id);
        }else{
            $merchant_list = null;
        }
        if($activity_id != 0 && $merchant_id != 0){
            $shop_list = D('Admin/Shop')->getMerchantShop($activity_id,$merchant_id);
        }else{
            $shop_list = null;
        }
         
        $map = array();
        //查询条件
        if($activity_id != 0){
            $map['activity_id'] = $activity_id;
        }
        
        // 开发者模式
        if(IS_ROOT == true){
            if($merchant_id != 0){
                $map['merchant_id'] = $merchant_id;
            }
        }else{
            // 商户获取
            $rules = M('auth_merchant')->where(array('member_id' => UID))->getField('rules');
            $allow_merchant = ($rules != "")? explode(',', $rules) : array();
            if($merchant_id != 0){
                $map['merchant_id'] = $merchant_id;
                if(!in_array($merchant_id, $allow_merchant)){
                    $this->error('暂无权限查看商户订单信息');
                }
            }else{
                if($allow_merchant){
                    $map['merchant_id'] = array('in', $allow_merchant);
                }else{
                    $this->error('暂无权限查看商户订单信息');
                }
            }
        }
        
        if($shop_id != 0){
            $map['shop_id'] = $shop_id;
        }
        if($order_type != 0){
            $map['order_type'] = $order_type;
        }
        if($order_status != 100){
            $map['order_status'] = $order_status;
        }
        if($paper_order != 0 ){
            $map['paper_order'] = $paper_order;
        }
        if(!empty($customer_info)){
            $map['customer_id'] = $customer_info['customer_id'];
        }
        if($order_id != 0){
            $map['order_id'] = $order_id;
        }
        if($coupons_type > -1){
            $map['coupons_type'] = $coupons_type;
        }
        if($apply_start_time > 0){
            $map['apply_time'] = array('between', array($apply_start_time, $apply_end_time));
        }
        if($back_start_time > 0){
            $map['back_time'] = array('between', array($back_start_time, $back_end_time));
        }
        // 搜索条件
        $search = ((count($map) == 1 && $map['merchant_id'] > 0))? false : true ;
        //列表
        if($search){
            //输入手机号不是系统用户时
            if($phone_number && empty($customer_info)){
                $list = null;
            }else{
                $list = $this->lists(D('OrderShopView'),$map,'create_time desc');
    	        // 查询日志
				echo action_log('order_select', 'order', UID, UID, $_SERVER['REQUEST_URI']);
            }
        }else{
            $list = null;
        }
    
        //返回值
        $this->assign('_list',$list);
        $this->assign('activity_list',$activity_list);
        $this->assign('merchant_list',$merchant_list);
        $this->assign('shop_list',$shop_list);
        $this->assign('url', U('Order/index'));
        $this->meta_title = '商户订单管理';
        $this->display('merchant_order');
    }
    
    /**
     * @todo 查看纸质订单所属商户,店铺名,品类名
     * @param paper_order_num int 纸质订单id
     * @author PHP@妖孽<dino_ma@163.com>
     */
    public function getPaperOrderInfo(){
    	if(IS_POST){
	    	$paper_order_num = (int)I('paper_order_num');
	    	if($paper_order_num ==0){
	    		$this->error('纸质订单Id有问题~');
	    	}
	    	$info = D('PaperOrderView')->field('paper_order_num,activity_id,shop_name,conf_category_id,merchant_id')->where(array('paper_order_num'=>$paper_order_num,'order_type'=>0))->find();
	    	if(empty($info)){
	    		$this->error('该订单信息不存在或已绑定.~');
	    	}else{
	    		$info['activity_name'] = getActivityName($info['activity_id']);
	    		$info['merchant_name'] = getMerchantName($info['merchant_id']);
	    		$info['conf_category'] = getConfCategoryName($info['conf_category_id']);
	    		unset($info['activity_id'],$info['merchant_id'],$info['conf_category_id']);
	    		$data['status']=1;
	    		$data['data']=$info;
	    		$this->ajaxReturn($data);
	    	}
    	}else{
    		$this->error('请不要非法访问哦.~');
    	}
    }
    
    /**
     * @todo    线上订单绑定线下纸质订单（包括自动销售空订单＋绑定当前展会活动️）
     * @author  fang
     */
    public function paperOrderBindingOrder(){
        if(IS_POST){
            $obj = D('Order');
            $result = $obj->paperOrderSaleBind();
            if($result){
                $this->success('操作成功');
            }else{
                $this->error($obj->getError());
            }
        }else{
            $this->error('参数错误');
        }
    }
    
    
    /**
     * @todo 打印线上订单收款凭证
     * @param order_id int 纸质订单id
     * @author PHP@妖孽<dino_ma@163.com>
     */
    public function printOnlineOrderVoucher(){
		$order_id = (int)I('order_id');
		if($order_id ==0){
			$this->error('线上订单Id有问题~');
		}
		$obj = D('Order');
		$order_info = $obj->orderShopShow($order_id,array('order_status'=>2,'order_type'=>2));
		if(empty($order_info)){
			$this->error('此线上订单暂未支付成功.~不能打印.~需要先绑定消费.~');
		}
		$order_snap = $obj->getOrderSnap($order_id);
		$this->assign('order_info',$order_info);
		$this->assign('order_snap',$order_snap);
		$this->display('print_online_pay');
    }
    
    /**
     * @todo 订单详情
     * @author sap
     */
    public function orderInfo(){
    	//获取数据
    	$order_id = (int)I('order_id');
    	if($order_id==0){
    		$this->error('参数有误.~');
    	}
    	//查询
    	$obj_order = D('Order');
    	$info = $obj_order->orderShopShow($order_id);//订单详情
    	$list = $obj_order->orderLogList(array('order_id'=>$order_id),'','create_time,log_id asc');//订单日志
    	$snap_info = $obj_order->getOrderSnap($order_id);
    	//返回值
    	$this->assign('list',$info);
    	$this->assign('_list',$list);
    	$this->assign('snap_info', $snap_info);
    	$this->assign('index_type', cookie('index_type'));
    	$this->meta_title_c = '订单管理';
    	$this->meta_title = '订单详情';
    	$this->display('info');
    }
    
    /**
     * @todo    商户订单详情
     * @author  fang
     */
    public function merchantOrderInfo(){
        //获取数据
        $order_id = (int)I('order_id');
        if($order_id==0){
            $this->error('参数有误.~');
        }
        //查询
        $obj_order = D('Order');
        $info = $obj_order->orderShopShow($order_id);//订单详情
        $list = $obj_order->orderLogList(array('order_id'=>$order_id),'','create_time,log_id asc');//订单日志
        $snap_info = $obj_order->getOrderSnap($order_id);
        //返回值
        $this->assign('list',$info);
        $this->assign('_list',$list);
        $this->assign('snap_info', $snap_info);
        $this->assign('index_type', cookie('index_type'));
        $this->meta_title_c = '商户订单管理';
        $this->meta_title = '商户订单详情';
        $this->display('merchant_order_info');
    }
    
    /**
     * @todo    获取申请核销店铺列表
     * @author  fang
     */
    public function getApplyShop(){
        
        // 活动列表
        $activity_list = D('Activity','Service')->getActivityList(0);
        // 获取申请核销店铺
        $apply_shop = D('CheckOrder')->getApplyShopList();
        $this->meta_title = '申请核销店铺';
        $this->assign('activity_list', $activity_list);
        $this->assign('apply_shop', $apply_shop);
        $this->display('get_apply_shop');
    }
   
    /**
     * @todo    质检结果查询
     * @author  fang
     */
    public function getCheckShop(){

        // 活动列表
        $activity_list = D('Activity','Service')->getActivityList(0);
        // 获取店铺列表
        $shop_list = D('Order')->getCheckShop();

        $this->meta_title = '质检结果查询';
        $this->assign('activity_list', $activity_list);
        $this->assign('shop_list', $shop_list);
        $this->display('get_check_shop');
    }
    
    /**
     * @todo 调用活动系统中的线下活动(公共方法)不受权限控制
     * @author   PHP@妖孽<dino_ma@163.com>
     * @param data_type   int  0:array,1:json
     */
    public function getActivityList($data_type){
    	if($data_type ==''){
    		$data_type = (int)I('data_type');
    	}
    	$data = array();
    	$activity_list = D('Activity','Service')->getActivityList(0);
    	switch ($data_type){
    		case 0:
    			return $activity_list;
    			break;
    		case 1://json
    			if($activity_list){
    				$data['status']=1;
    				$data['data']=$activity_list;
    			}else{
    				$data['status']=0;
    				$data['info']='么么哒~';
    			}
    			$this->ajaxReturn($data);
    			break;
    		default:
    			$this->error('参数有误~');
    			break;
    	}
    
    }
    
    
    
}